package cn.swing.his.passport.security.config;

import cn.swing.his.passport.security.MyAccessDeniedHandler;
import cn.swing.his.passport.security.MyAuthenticationEntryPoint;
import cn.swing.his.passport.security.filter.SSOFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.CorsConfigurer;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

@Configuration
public class SSOWebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private SSOFilter ssoFilter;
    @Autowired
    private MyAccessDeniedHandler myAccessDeniedHandler;
    @Autowired
    private MyAuthenticationEntryPoint myAuthenticationEntryPoint;

    @Bean
    CorsConfigurationSource corsConfigurationSource(){
        CorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 同源配置，*表示任何请求都视为同源，若需指定ip和端口介意改为如"localhost:8080",多个以","分隔
        corsConfiguration.addAllowedOrigin("*");
        // header，允许哪些header
        corsConfiguration.addAllowedOrigin("Authorization");
        // 允许的请求方法，post、get等
        corsConfiguration.addAllowedMethod("*");
        // 配置允许跨域访问的url
        ((UrlBasedCorsConfigurationSource) source).registerCorsConfiguration("/**",corsConfiguration);
        return source;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 放行清单
        String[] permitList = {
                "/**",
                "/swagger-resource",
                "/v2/api-docs",
                "/doc.html",
                "/favicon.ico",
                "/**/*.css",
                "/**/*.js"
        };
        // 禁用跨域请求伪造过滤器
        http.csrf().disable();
        http.cors().configurationSource(corsConfigurationSource());
        // 关闭跨域
        CorsConfigurer<HttpSecurity> cors = http.cors();
        // 配置各请求路径的认证与授权
        http.authorizeRequests() // 请求需要授权才可以访问
                .antMatchers(permitList) // 匹配一些路径
                .permitAll()
                .antMatchers(HttpMethod.OPTIONS)
                .permitAll() // 允许直接访问
                .anyRequest()
                .authenticated(); // 都需要认证

        // 权限不足处理器
        http.exceptionHandling().accessDeniedHandler(myAccessDeniedHandler);
        // 未认证处理器
        http.exceptionHandling().authenticationEntryPoint(myAuthenticationEntryPoint);

        // 注册处理JWT的过滤器
        // 此过滤器必须在Spring Security处理登录的过滤器之前
        http.addFilterBefore(ssoFilter, UsernamePasswordAuthenticationFilter.class);
    }
}
